package servlet.rool;

import net.sf.json.JSONObject;
import service.RoMessService;
import service.RoleService;
import service.impl.RoMessServiceImpl;
import service.impl.RoleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 批量删除角色的servlet
 */
@WebServlet(value = "/RoleDeleAllServlet")
public class RoleDeleAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/json;charset=UTF-8");
        String id= request.getParameter("str");
        RoleService rs=new RoleServiceImpl();
        RoMessService rms=new RoMessServiceImpl();
        int bo=0;
        int bo2=0;
        //将页面接收到的值进行拆分
        String cheid[]=id.split(",");
        //拆分后循环运行方法
        for(int i=0;i<cheid.length;i++){
            bo =rs.deleRole(Integer.parseInt(cheid[i]));
            bo2=rms.deleRmass(Integer.parseInt(cheid[i]));
        }
        JSONObject json=new JSONObject();
        if(bo>0 && bo2>0){
            json.put("msg","删除成功");
        }else {
            json.put("msg","删除失败");
        }
        PrintWriter out=response.getWriter();
        out.print(json);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        doGet(request, response);
    }
}
